#include "broadcast_core.cpp"
#include "../include/utils/header_global.h"
#include <cstring>
#include <iostream>
#include <string.h>
#include <string>

// =============== Windows ==============
#if defined(_WIN32) || defined(_WIN64)
#include <cstdint>
#include <ws2tcpip.h>
#include <winsock2.h>
#include "ws2ipdef.h"
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#define close(sock) closesocket(sock)
typedef SOCKET socket_t;
#define INVALID_SOCKET_VALUE INVALID_SOCKET
using ushort = unsigned short;
// =========  Unix/Linux/Macos  =========
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ifaddrs.h> //ipv4
#include <netdb.h>   //ipv4
#include <sys/select.h>
#define INVALID_SOCKET_VALUE -1
typedef int             socket_t;
typedef struct sockaddr SOCKADDR;
#endif

using namespace std;

// 编译
// cd tools
// Linux  : g++ -std=c++11 -pthread -I../include -o ../bin/broadcast_publisher.exe broadcast_publisher.cpp
// Windows: g++ -std=c++11 -pthread -I../include -o ../bin/broadcast_publisher.exe broadcast_publisher.cpp -lws2_32
int main(const int argc, char *argv[]) {
#ifdef _WIN32
	SetConsoleOutputCP(CP_UTF8);
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		std::cerr << "WSAStartup failed..." << std::endl;
		return 1;
	}
#endif
	string ip   = "255.255.255.255";
	int    port = 9999;

	if (argc == 3) {
		ip   = argv[1];
		port = std::stoi(argv[2]);
	} else if (argc == 2) {
		port = std::stoi(argv[1]);
	} else {
		cout << "Usage: ./broadcast_publisher [broadcas_ip] <broadcas_port>" << endl;
		return 1;
	}
	Broadcast b;
	b.publish(ip, port, "hello world");
#ifdef _WIN32
	WSACleanup();
#endif
	getchar();
	return 0;
}
